﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Resources;
using System.Reflection;
using System.Globalization;

namespace TagCloudSoma
{
    public class ProviderPattern
    {
        string[] _patterns;
        ArrayList _d;

        public ProviderPattern()
        {
            Initialize();
            _d.Sort();
            ResposeBuilder();
        }

        #region Private Methods
        
        private void Initialize()
        {
            ResourceManager rsxr = new ResourceManager("tagcloudsoma.resources.patterns", GetType().Assembly);
            ResourceSet rs = rsxr.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
            _d = new ArrayList();

            Pattern p;
            int order;
            string key, value;
            IDictionaryEnumerator ide = rs.GetEnumerator();

            while (ide.MoveNext())
            {
                order = int.MaxValue;
                key = (string)ide.Key;
                value = (string)ide.Value;

                if (ide.Key.ToString().Contains("_"))
                {
                    string[] a = ide.Key.ToString().Split('_');
                    //order = Convert.ToInt32(a[0]);
                    if (!int.TryParse(a[0], out order))
                    {
                        order = int.MaxValue;
                    }
                    key = a[1];
                }
                else
                {
                    key = (string)ide.Key;
                }

                p = new Pattern(order, key, value);
                _d.Add(p);
            }
        }

        private void ResposeBuilder()
        {
            int i = 0;
            _patterns = new string[_d.Count];

            foreach (Pattern entry in _d)
            {
                _patterns[i] = entry.Pat;
                i++;
            }
        }
        
        #endregion

        #region Properties
        public string[] Patterns
        {
            get { return _patterns; }
        }

        public int size()
        {
            return _patterns.Length;
        }
        #endregion
    }
}
